package com.newjiabo.ski.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.newjiabo.common.ski.media.module.enums.MamEntityIdEnum;
import com.newjiabo.common.ski.media.module.enums.MediaOwnerTypeEnum;
import com.newjiabo.common.ski.media.module.enums.MediaStatus;
import com.newjiabo.common.ski.media.module.enums.MediaType;
import com.newjiabo.common.ski.media.utils.MediaRequestService;
import com.newjiabo.common.utils.SecurityUtils;
import com.newjiabo.ski.domain.SkiMemberBodyInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * {类内容}
 *
 * @author CaoMengHui.
 * @version 1.0
 * @since 2023/9/21 16:11
 */
@Service
public class MediaService {

    @Resource
    private MediaRequestService mediaRequestService;
    @Resource
    private ISkiMemberBodyInfoService memberBodyInfoService;

    @Transactional(rollbackFor = Exception.class)
    public SkiMemberBodyInfo getRegisterInfo(String snowFieldId) {
        SkiMemberBodyInfo memberBodyInfo = memberBodyInfoService.getOneRecentBySnowFieldId(snowFieldId);
        if (ObjectUtil.isNull(memberBodyInfo)) {
            return memberBodyInfo;
        }
        if (MediaStatus.Normal.name().equals(memberBodyInfo.getRegisterStatus())) {
            return memberBodyInfo;
        }
        String userId = SecurityUtils.getMemberId();
        String ownerType = MediaOwnerTypeEnum.Private.name();
        String mediaType = MediaType.Image.name();
        String mamEntityId = MamEntityIdEnum.PersonPhoto.name();
        JSONObject jsonObject = mediaRequestService.searchMedia(userId, ownerType, mediaType, mamEntityId,
                null, MediaRequestService.getExtendMatchStr(snowFieldId), null, 1, 50);
        Boolean fag = true;
        Map<String, String> mediaMap = new HashMap<>(3);
        if (jsonObject.getLong("total") > 0) {
            JSONArray mediaInfoList = jsonObject.getJSONArray("mediaInfoList");
            for (int i = 0; i < mediaInfoList.size(); i++) {
                JSONObject mediaInfo = mediaInfoList.getJSONObject(i);
                if (MediaStatus.Normal.name().equals(mediaInfo.getString("status"))) {
                    mediaMap.put(mediaInfo.getString("mediaId"), mediaInfo.getString("coverUrl"));
                } else {
                    fag = false;
                }
            }
        }
        if (fag) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : mediaMap.entrySet()) {
                if (entry.getKey().equals(memberBodyInfo.getFrontalViewMediaId())) {
                    memberBodyInfo.setFrontalView(entry.getValue());
                } else {
                    sb.append(entry.getValue());
                    sb.append(",");
                }
            }
            sb.substring(0, sb.length() - 2);
            memberBodyInfo.setFullBodyPhoto(sb.toString());
            memberBodyInfo.setRegisterStatus(MediaStatus.Normal.name());
            memberBodyInfoService.updateSkiMemberBodyInfo(memberBodyInfo);
        }
        return memberBodyInfo;
    }

}
